package com.sinopec.android.net;

import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.sinopec.android.R;
import com.sinopec.android.bean.ResultData;
import com.sinopec.android.bean.ThreadCallBack;
import com.sinopec.android.constant.ConfigAttrs;
import com.sinopec.android.util.LogUtil;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class ThreadManger {
	public static Dialog dialog;
	private static Context mContext;
	public static Thread thread = null;
	public static boolean isGo = true;
	public static ExecutorService threadPool = Executors.newFixedThreadPool(2);
	
	static Handler handler = new Handler() {
		@SuppressWarnings("rawtypes")
		public void handleMessage(Message msg) {
			ResultData resultData = (ResultData) msg.obj;
			ThreadCallBack callBack = (ThreadCallBack) msg.getData()
					.getSerializable("callback");
			callBack.onCallbackFromThread(resultData);
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}
		}
	};
	
	@SuppressWarnings("rawtypes")
	public static void exeTask(final ThreadCallBack callBack, final int taskId,
			final HashMap hashMap, final String reqUrl) {
		isGo = true;
		mContext = (Context) callBack;
		if (!HttpManager.checkNetWork(mContext)) {
			return;
		}
		//透明loading进度条
		dialog = new Dialog(mContext,R.style.NobackDialog);  
		dialog.setContentView(R.layout.dialog_loading);  
		TextView loadTip = (TextView) dialog.findViewById(R.id.loadTip);
		loadTip.setText(mContext.getResources().getString(R.string.loadingTip));
		
		dialog.show();
		
		dialog.setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface arg0) {
				LogUtil.printInfo("the dialog is cancel");
				//终止请求进程
				isGo = false;
			}
		});
		
		thread = new Thread(new Runnable() {
			@Override
			public void run() {
					ResultData resultData = sendToServer(taskId, hashMap, reqUrl);
					if(isGo){
						Message msg = new Message();
						msg.arg1 = taskId;
						msg.obj = resultData;
						msg.getData().putSerializable("callback", callBack);
						handler.sendMessage(msg);
					}
			}
		});
		isGo = true;
		threadPool.execute(thread);
	}
	
	@SuppressWarnings("rawtypes")
	public static ResultData sendToServer(int taskId,
			HashMap<String, String> hashMap, String reqUrl) {
		ResultData resultData = null;
		String response;
		switch (taskId) {

		case ConfigAttrs.LOGIN_TASK_FLAG:// 登录信息
			response = HttpManager.doPost(reqUrl, hashMap);
//			response="{\"result\":{\"errorCode\":\"\",\"message\":\"\"},\"userName\":\"刘汝山\",\"annouceToDo\":402,\"documentToDo\":0,\"travelToDo\":4}";
			resultData = ParseManger.getLoginUserInfo(response, mContext);
			break;
		case ConfigAttrs.QUERY_ADDRESSBOOK_TASK_FLAG://查询通讯录
			response = HttpManager.doPost(reqUrl, hashMap);
			resultData = ParseManger.getAddressBookList(response,mContext);
			break;
		case ConfigAttrs.TODO_THING_TASK_FLAG://待办事宜
			response = HttpManager.doPost(reqUrl, hashMap);
//			response= "{\"result\":{\"errorCode\":\"\",\"message\":\"\"},\"account\":337,\"list\":[{\"type\":\"收文审批\",\"name\":\"赵金海\",\"title\":\"加班申请\",\"date\":\"2012-03-25\",\"url\":\"4393\",\"bussinessID\":\"944\"},{\"type\":\"正职出差申请审批\",\"name\":\"赵金海\",\"title\":\"加班申请\",\"date\":\"2012-03-25\",\"url\":\"4561\",\"bussinessID\":\"319\"}]}";
			resultData = ParseManger.getToDoThingList(response,mContext);
			break;
		case ConfigAttrs.DOCUMENT_DETAIL_TASK_FLAG://公文详情
			response = HttpManager.doPost(reqUrl, hashMap);
//			response= "{\"AttDocList\":[{\"typeName\":\"正文\",\"attFieleName\":\"发文的盖章正文 - 副本.xls\",\"attFilePath\":\"http://10.230.38.7:8012/Upload/DocumentTemp/2012/SWZW_b1604082-01db-49e5-87fe-764b5e107628.xls\"}],\"dispathBasic\":{\"recData\":\"2012.20.07\",\"recNo\":\"第977号\",\"docNo\":\"石化股份〔2012〕号\",\"recDeport\":\"测试-7.19\",\"recTitle\":\"测试-7.19\",\"remark\":\"\",\"tileState\":\"\"},\"processIdeaList\":[{\"JobStep\":2,\"SignComment\":\"请院领导审批（手机签批）\",\"SignTime\":\"2012-07-20\",\"SignUserName\":\"\",\"SignUserPath\":\"http%3a%2f%2f10.230.38.7%3a8012%2fSignImage%2f%e9%97%ab%e7%8e%b0%e5%86%9b.png\"},{\"JobStep\":10,\"SignComment\":\"同意。\",\"SignTime\":\"2012-07-20\",\"SignUserName\":\"孟民\",\"SignUserPath\":\"\"}],\"JobStepID\":4,\"result\":{\"errorCode\":\"\",\"message\":\"\"},\"InsID\":\"9304\",\"BtnType\":0}";
//			response = "{\"AttDocList\": [{\"typeName\": \"正文\",\"attFieleName\": \"如何发短信.pdf\",\"attFilePath\": \"http://10.230.38.7:8012/Upload/DocumentTemp/2012/SWZW_6ca2efcc-6a7d-462d-8194-082819dd51ba如何发短信.pdf\"},{\"typeName\": \"second文\",\"attFieleName\": \"如何发短信sdsdsdsdsdsddsecond.pdf\",\"attFilePath\": \"http://10.230.38.7:8012/Upload/DocumentTemp/2012/SWZW_6ca2efcc-6a7d-462d-8194-082819dd51ba如何发短信second.pdf\"},{\"typeName\": \"three文\",\"attFieleName\": \"如何发短信three.pdf\",\"attFilePath\": \"http://10.230.38.7:8012/Upload/DocumentTemp/2012/SWZW_6ca2efcc-6a7d-462d-8194-082819dd51ba如何发短信three.pdf\"}],\"dispathBasic\": {\"recData\": \"2012.01.04\",\"recNo\": \"第14号\",\"docNo\": \"石化股份〔2012〕\",\"recDeport\": \"中石化\",\"recTitle\": \"移动OA测试2\",\"remark\": \"\",\"tileState\": \"\"},\"processIdeaList\": [{\"JobStep\": 2,\"SignComment\": \"请刘书记阅示。\",\"SignTime\": \"2012-04-01\",\"SignUserName\": \"闫现军\",\"SignUserPath\": \"\"},{\"JobStep\": 2,\"SignComment\": \"请刘书记阅示。\",\"SignTime\": \"2012-04-01\",\"SignUserName\": \"\",\"SignUserPath\": \"闫现军\"},{\"JobStep\": 10,\"SignComment\": null,\"SignTime\": \"0001-01-01\",\"SignUserName\": \"孟民\",\"SignUserPath\": \"\"},{\"JobStep\": 4,\"SignComment\": \"Nguni//手机签批\",\"SignTime\": \"2012-04-06\",\"SignUserName\": \"刘汝山\",\"SignUserPath\": \"\"}],\"JobStepID\": 10,\"result\": {\"errorCode\": \"\",\"message\": \"\"},\"InsID\": \"4398\",\"BtnType\": 0}";
			resultData = ParseManger.getDocumentDetail(response,mContext);
			break;
		case ConfigAttrs.BUSINESS_TRIP_TASK_FLAG://出差详情
			response = HttpManager.doPost(reqUrl, hashMap);
//			response = "{\"KqglBasic\":{\"EvectionUserName\":\"肖莉\",\"EvectionDeport\":\"信息与标准化研究所\",\"EvectionStartTime\":\"2012-03-26\",\"EvectionEndTime\":\"2012-03-30\",\"EvectionAddress\":\"东营\",\"EvectionReason\":\"培训\",\"EvectionRideLine\":\"北京-东营\"},\"KqglProcessList\":[{\"JobStep\":\"0\",\"SignComment\":\"同意\",\"SignTime\":\"2012-03-25\",\"SignUserName\":\"赵金海\",\"SignUserPath\":\"\"},{\"JobStep\":\"1\",\"SignComment\":\"参加(手机签批)\",\"SignTime\":\"2012-03-25\",\"SignUserName\":\"刘汝山\",\"SignUserPath\":\"\"}],\"result\":{\"errorCode\":\"\",\"message\":\"\"},\"InsID\":\"4562\",\"btnState\":0,\"btnStop\":\"0\",\"btnRetrieve\":\"0\"}";
			resultData = ParseManger.getBussinessTrip(response,mContext);
			break;
		case ConfigAttrs.SUBMIT_BUSINESS_TRIP_TASK_FLAG://提交或退回出差公文
			response = HttpManager.doPost(reqUrl, hashMap);
//			response = "{\"result\":{\"errorCode\":\"\",\"message\":\"\"}}" ;
			resultData = ParseManger.getSubmitBussinessTrip(response,mContext);
			break;
			
		case ConfigAttrs.CONFERENCE_MANAGEMENT_TASK_FLAG://会议管理列表
			response = HttpManager.doPost(reqUrl, hashMap);
//			response= "{\"result\":{\"errorCode\":\"\",\"message\":\"\"},\"account\":337,\"weekdate\":\"2012-04-09 至2012-04-16 (第15周)  工作安排\",\"list\":[{\"id\":\"1\",\"title\":\"OA一期、二期集成专题会\",\"time\":\"2012-04-25 14:30至2012-04-25 15:30\",\"address\":\"798艺术中心\"},{\"id\":\"2\",\"title\":\"OA一期、二期集成专题会\",\"time\":\"2012-04-25 14:30至 2012-04-25 15:30\",\"address\":\"798艺术中心\"},{\"id\":\"3\",\"title\":\"OA一期、二期集成专题会\",\"time\":\"2012-04-25 14:30至 2012-04-25 15:30\",\"address\":\"798艺术中心\"},{\"id\":\"4\",\"title\":\"OA一期、二期集成专题会\",\"time\":\"2012-04-25 14:30至 2012-04-25 15:30\",\"address\":\"798艺术中心\"},{\"id\":\"5\",\"title\":\"OA一期、二期集成专题会\",\"time\":\"2012-04-25 14:30至 2012-04-25 15:30\",\"address\":\"798艺术中心\"},{\"id\":\"6\",\"title\":\"OA一期、二期集成专题会\",\"time\":\"2012-04-25 14:30至 2012-04-25 15:30\",\"address\":\"798艺术中心\"},{\"id\":\"7\",\"title\":\"OA一期、二期集成专题会\",\"time\":\"2012-04-25 14:30至 2012-04-25 15:30\",\"address\":\"798艺术中心\"},{\"id\":\"8\",\"title\":\"OA一期、二期集成专题会\",\"time\":\"2012-04-25 14:30至 2012-04-25 15:30\",\"address\":\"798艺术中心\"}]}";
			resultData = ParseManger.getConferenceManagementList(response,mContext);
			break;
		case ConfigAttrs.LEADERSHIP_AGENDA_TASK_FLAG://领导日程
			response = HttpManager.doPost(reqUrl, hashMap);
			resultData = ParseManger.getLeaderShipAgendaList(response,mContext);
			break;	
			
		}
		return resultData;
	}
}
